package com.bw.find_module.recommend;

import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.bw.adapter_sdk.quick.BaseQuickAdapter;
import com.bw.common.entity.LiveDataEntity;
import com.bw.common.mvvm.view.BaseFragment;
import com.bw.find_module.R;
import com.bw.find_module.databinding.FragmentFindBinding;
import com.bw.find_module.databinding.FragmentRecommendBinding;
import com.bw.find_module.entity.RecommendEntity;
import com.bw.find_module.entity.hasattent.HasEntity;

import org.greenrobot.eventbus.EventBus;

public class RecommendFragment extends BaseFragment<FragmentRecommendBinding, RecommendViewModel> implements BaseQuickAdapter.OnItemClickChildListener {

    private RecommendAdapter recommendAdapter;
    private int position = -1;

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        return FragmentRecommendBinding.inflate(inflater);
    }

    @Override
    public void initView() {
        v.recommendRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        recommendAdapter = new RecommendAdapter();
        recommendAdapter.setOnItemClickChildListener(this);
        v.recommendRecycler.setAdapter(recommendAdapter);
    }

    @Override
    public void initData() {

    }

    @Override
    public void handleMessage(LiveDataEntity entity) {
        switch (entity.action) {
            case START_VIEW_MODEL:
                RecommendEntity recommendEntity = (RecommendEntity) entity.data.get(LiveDataEntity.LiveDataType.ENTITY);
                Log.i("hxw", recommendEntity.toString());
                recommendAdapter.setNewInstance(recommendEntity.data.data);
                break;
            case IS_ATTENTION:
                HasEntity hasEntity = (HasEntity) entity.data.get(LiveDataEntity.LiveDataType.ENTITY);
                recommendAdapter.getData().get(position).hasFollow = hasEntity.data.data.hasFollow;
                recommendAdapter.notifyItemChanged(position);
                EventBus.getDefault().post("refreshAttent");
                break;
        }
    }

    @Override
    public void onItemClickChild(RecyclerView.Adapter adapter, int id, int position) {
        RecommendAdapter recommendAdapters = (RecommendAdapter) adapter;
        if (id == R.id.item_recommend_attent) {
            this.position = position;
            LogUtils.e("tagId" + recommendAdapters.getData().get(position).tagId);
            vm.onHas(recommendAdapters.getData().get(position).tagId);
        } else if (id == R.id.item_recommend_artist_img) {
            ARouter.getInstance().build("/find/userdetailactivity").withSerializable("recommentEntity", recommendAdapters.getData().get(position)).navigation();
        }
    }
}
